package com.zaofan.controller;

import java.io.IOException;
import java.sql.Connection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.bean.AccountLog;
import com.zaofan.bean.OrderInfo;
import com.zaofan.constant.StateConstant;
import com.zaofan.service.AccountLogService;
import com.zaofan.utils.C3p0DataSourceUtils;
import com.zaofan.utils.JsonUtils;
import com.zaofan.utils.WebUtils;

import org.apache.commons.dbutils.QueryRunner;

@WebServlet("/accountLog")
public class AccountLogController extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private AccountLogService accountLogService = new AccountLogService();
	

	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Map<String, Object> params = new HashMap<>();
			Enumeration<String> enumeration = request.getParameterNames();
			while (enumeration.hasMoreElements()) {
				String name = enumeration.nextElement();
				if ("action".equals(name)) {
					continue;
				}
				Object value = request.getParameter(name);
				params.put(name, value);
			}
			List<AccountLog> logs = accountLogService.findAll(params);
			Long count = accountLogService.count(params);
			JsonUtils.writeSuccessPageJson("查询成功", logs, count, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}

	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			AccountLog log = WebUtils.write2bean(request, AccountLog.class);
			accountLogService.addByAdmin(log);
			JsonUtils.writeSuccessJson("添加成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("添加失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("添加失败!!", response);
	}

	protected void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			AccountLog log = WebUtils.write2bean(request, AccountLog.class);
			accountLogService.update(log);
			JsonUtils.writeSuccessJson("修改成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("修改失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("修改失败!!", response);
	}

	protected void get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String id_s = request.getParameter("id");
			Integer id = Integer.valueOf(id_s);
			AccountLog log = accountLogService.findById(id);
			JsonUtils.writeSuccessJson("查询成功", log, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询成功!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("查询成功!!", response);
	}

	protected  void getreturnlist(HttpServletRequest request,HttpServletResponse response) throws  ServletException,IOException{
		try {
			Map<String, Object> params = new HashMap<>();
			Enumeration<String> enumeration = request.getParameterNames();
			while (enumeration.hasMoreElements()) {
				String name = enumeration.nextElement();
				if ("action".equals(name)) {
					continue;
				}
				Object value = request.getParameter(name);
				params.put(name, value	);
				params.put("mark","fz");
			}

			List<AccountLog> logs= accountLogService.findAll(params);
			Long count = accountLogService.count(params);
			JsonUtils.writeSuccessPageJson("查询成功", logs, count, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询成功!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("查询成功!!", response);
	}

	protected  void returndo(HttpServletRequest request,HttpServletResponse response) throws  ServletException,IOException{
		Connection connection = C3p0DataSourceUtils.getConnection();
		int id = Integer.parseInt(request.getParameter("id"));

		try {
			connection.setAutoCommit(false);
			QueryRunner runner = new QueryRunner();
			String sql = "update accountlog set mark =" +"'fz0'"+ " where id = " +id;
			int count = runner.update(connection, sql);
			if (count>=1){
				JsonUtils.writeSuccessJson("查询成功", count, response);
			}else{
				JsonUtils.writeSuccessJson("查询失败", count, response);
			}
			connection.commit();
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询错误!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!", response);
	}

	protected  void returnundo(HttpServletRequest request,HttpServletResponse response) throws  ServletException,IOException{
		Connection connection = C3p0DataSourceUtils.getConnection();
		int id = Integer.parseInt(request.getParameter("id"));
		try {
			connection.setAutoCommit(false);
			QueryRunner runner = new QueryRunner();
			String sql = "update accountlog set mark =" +"'fz'"+ " where id = " +id;
			int count = runner.update(connection, sql);
			if (count>=1){
			JsonUtils.writeSuccessJson("查询成功", count, response);
			}else{
				JsonUtils.writeSuccessJson("查询失败", count, response);
			}
			connection.commit();
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询错误!!" + e.getMessage(), response);
		}

		JsonUtils.writeBeanFailJson("查询失败!!", response);
	}

}
